/**
 * 应用基础视图
 */
define(['jquery', 'underscore', 'RootView'],
    function ($, _, RootView, introJs) {
        var MainView = RootView.extend({
            el: 'body',
            events: {},
            initialize: function () {
                if ($('#ChannelChart').length > 0) {
                    this.generateChannelChart();
                }
                this.generateTagCloudChart();
            },

            //# 初始化今日单量
            //
            generateChannelChart: function () {
                var $ChannelChart = $('#ChannelChart'),
                    sourceUrl = $ChannelChart.data('source-url'),
                    that = this;

                var channelChart = that.initEchart($('#ChannelChart'));
                var option = {
                    title: {
                        text: '总单量',
                        x: 'center',
                        y: 'center',
                        itemGap: 20,
                        textStyle: {
                            fontSize: 20,
                            color: '#292929'
                        },
                        subtextStyle: {
                            fontSize: 40,
                            fontWeight: 'bolder',
                            color: '#292929'
                        }
                    },
                    legend: {
                        orient: 'vertical',
                        x: 'right',
                        y: 'bottom',
                        data: []
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    calculable: false,
                    series: [{
                        name: '总单量',
                        type: 'pie',
                        radius: ['50%', '80%'],
                        itemStyle: {
                            normal: {
                                label: {
                                    position: 'outer',
                                    formatter: function (params) {
                                        return params.name;
                                    }
                                },
                                labelLine: {
                                    show: true,
                                    formatter: function (params) {
                                        console.log(params);
                                    }
                                }
                            },
                            emphasis: {
                                label: {
                                    show: false
                                }
                            }

                        }
                    }]
                };

                $.ajax({
                    url: sourceUrl,
                    type: 'get',
                    dataType: 'json',
                    beforeSend: function (req) {
                        req.setRequestHeader("Accept", "application/json");
                    },
                }).done(function (res) {
                    if (res.Errors && !res.Errors.IsValid) {
                        that.errorAlert(res.Errors.ErrorItems[0].ErrorMessage);
                    } else {
                        var orderTotal = 0;
                        if (res.Content.data.length > 0) {
                            _.each(res.Content.data, function (order) {
                                orderTotal += order.value;
                                option.legend.data.push(order.name);
                            });
                            option.series[0].data = res.Content.data;
                            option.title.subtext = orderTotal;
                        } else {
                            option.color = ['#f96868']
                            option.tooltip = null;
                            option.series[0].data = [{
                                name: "所有订单",
                                value: 0
                            }]
                            option.title.subtext = '0';
                        }
                        channelChart.setOption(option);
                    }
                }).fail(function () {
                    that.errorAlert('网络加载超时');
                }).always(function () {
                });

                return channelChart;
            },
            generateTagCloudChart: function () {
                var $TagCloudChart = $('#TagCloudChart'),
                    sourceUrl = $TagCloudChart.data('source-url'),
                    that = this;

                var tagTpl = _.template('<a class="tag" href="<%=url%>" style="color:<%=color%>" target="_blank"><%=name%></a>');

                $.ajax({
                    url: sourceUrl,
                    type: 'get',
                    dataType: 'json',
                    beforeSend: function (req) {
                        req.setRequestHeader("Accept", "application/json");
                    },
                }).done(function (res) {
                    if (res.Errors && !res.Errors.IsValid) {
                        that.errorAlert(res.Errors.ErrorItems[0].ErrorMessage);
                    } else {
                        if (res.Content.data.length > 0) {
                            _.each(res.Content.data, function (tagData) {
                                $TagCloudChart.append(tagTpl(tagData));
                            });
                        } else {
                            $TagCloudChart.append('<span style="font-size:14px;padding:10px;">『加标签』关注特别订单</span>');
                        }

                    }
                }).fail(function () {
                    that.errorAlert('网络加载超时');
                }).always(function () {
                });
            },

        });
        return MainView;
    });
